package uit.com.project.iCampus.xmlparser;

import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import uit.com.project.iCampus.model.BuildingInfo;
import uit.com.project.iCampus.model.User;
import uit.com.project.iCampus.model.UserOnline;

public class XMLParserAndroidUtil {
	public static SAXParserFactory parserFactory;
	public static SAXParser parser;
	public static XMLReader xmlReader;
	public static String ROOT_ELEMENT = "List";
	public static String ELEMENT = "Item";

	/**
	 * unmarshal User
	 * @param str
	 * @return
	 */
	public static User unmarshalUser(String str) {
		User user = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			UserXMLHandler handler = new UserXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			user = handler.getUser();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	
	public static List<BuildingInfo> unmarshalListBuildngInfo(String stringXML) {
		List<BuildingInfo> results = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			BuildingInfoXMLHandler handler = new BuildingInfoXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(stringXML);
			xmlReader.parse(new InputSource(stringReader));
			results = handler.getBuildingInfos();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}
	
	public static List<UserOnline> unmarshalListUserOnline(String stringXML) {
		List<UserOnline> results = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			UserOnlineXMLHandler handler = new UserOnlineXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(stringXML);
			xmlReader.parse(new InputSource(stringReader));
			results = handler.getUserOnlines();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}

	/*
	public static List<NguoiDung> unmarshalListNguoiDung(String str) {
		List<NguoiDung> dsNguoiDung = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			NguoiDungXMLHandler handler = new NguoiDungXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			dsNguoiDung = handler.getDsNguoiDung();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dsNguoiDung;
	}
	
	public static CauHoiTrucTuyen unmarshalCauHoiTrucTuyen(String str) {
		CauHoiTrucTuyen cauHoi = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			CauHoiTrucTuyenXMLHandler handler = new CauHoiTrucTuyenXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			cauHoi = handler.getCauHoi();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cauHoi;
	}
	
	public static List<CauHoiTrucTuyen> unmarshalListCauHoiTrucTuyen(String str) {
		List<CauHoiTrucTuyen> dsCauHoi = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			CauHoiTrucTuyenXMLHandler handler = new CauHoiTrucTuyenXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			dsCauHoi = handler.getDsCauHoi();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dsCauHoi;
	}
	
	public static List<String> unmarshalListChuDe(String str){
		List<String> dsChuDe = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			ChuDeXMLHandler handler = new ChuDeXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			dsChuDe = handler.getDsChuDe();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dsChuDe;
	}
	
	public static NhomNguoiDung unmarshalNhomNguoiDung(String str) {
		NhomNguoiDung nhomND = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			NhomNguoiDungXMLHandler handler = new NhomNguoiDungXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			nhomND = handler.getNhomNguoiDung();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nhomND;
	}
	
	public static List<NhomNguoiDung> unmarshalListNhomNguoiDung(String str) {
		List<NhomNguoiDung> dsNhomNguoiDung = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			NhomNguoiDungXMLHandler handler = new NhomNguoiDungXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			dsNhomNguoiDung = handler.getDsNhomNguoiDung();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dsNhomNguoiDung;
	}
	
	public static BaiViet unmarshalBaiViet(String str){
		BaiViet baiViet = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			BaiVietXMLHandler handler = new BaiVietXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			baiViet = handler.getBaiViet();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baiViet;
	}
	
	public static List<BaiViet> unmarshalListBaiViet(String str){
		List<BaiViet> list = new ArrayList<BaiViet>();
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			BaiVietXMLHandler handler = new BaiVietXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			list = handler.getDsBaiViet();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static YKienBanDoc unmarshalYKienBanDoc(String str){
		YKienBanDoc yKienBanDoc = null;
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			YKienBanDocXMLHandler handler = new YKienBanDocXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			yKienBanDoc = handler.getyKienBanDoc();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return yKienBanDoc;
	}
	
	public static List<YKienBanDoc> unmarshalListYKienBanDoc(String str){
		List<YKienBanDoc> list = new ArrayList<YKienBanDoc>();
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			YKienBanDocXMLHandler handler = new YKienBanDocXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			list = handler.getDsYKienBanDoc();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static List<String> unmarshalListChuDeKhac(String str){
		List<String> list = new ArrayList<String>();
		try {
			parserFactory = SAXParserFactory.newInstance();
			parser = parserFactory.newSAXParser();
			xmlReader = parser.getXMLReader();
			ChuDeKhacXMLHandler handler = new ChuDeKhacXMLHandler();
			xmlReader.setContentHandler(handler);
			StringReader stringReader = new StringReader(str);
			xmlReader.parse(new InputSource(stringReader));
			list = handler.getDsChuDeKhac();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}*/

}
